package com.example.messenger;

import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class MessagesAdapter extends BaseAdapter implements OnClickListener {
    private Context context;

    private List<Messages> listMessages;

    public MessagesAdapter(Context context, List<Messages> listMessages) {
        this.context = context;
        this.listMessages = listMessages;
    }

    public int getCount() {
        return listMessages.size();
    }

    public Object getItem(int position) {
        return listMessages.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup viewGroup) {
        Messages entry = listMessages.get(position);
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.list_items, null);
        }
        TextView nameView = (TextView) convertView.findViewById(R.id.nameView);
        nameView.setText(entry.getSender());

        TextView timeView = (TextView) convertView.findViewById(R.id.timeView);
        timeView.setText(entry.getTime());

        TextView messageView = (TextView) convertView.findViewById(R.id.messageView);
        messageView.setText(entry.getShortMessage());
        return convertView;
    }

    @Override
    public void onClick(View view) {
        Messages entry = (Messages) view.getTag();
        listMessages.remove(entry);
        // listMessages.remove(view.getId());
        notifyDataSetChanged();
    }
}
